﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using RemoteObject;
using System.Windows.Forms;

namespace RemoteClient.UI
{
    public partial class SegmentItem : ListViewItem
    {
        
        private DownloadWorkder downloader;
        private ListViewSubItem startPosItem;
        private ListViewSubItem endPosItem;
        private ListViewSubItem processItem;
        private ListViewSubItem rateItem;

        public DownloadWorkder Downloader
        {
            get
            {
                return downloader;
            }
            set 
            {
                downloader = value;
            }
        }

        private Timer updateTimer;

        public SegmentItem(DownloadWorkder downloader)
        {
            this.downloader = downloader;                  
        }

        public void Dispose()
        {
            if (updateTimer != null)
            {
                updateTimer.Stop();
                updateTimer.Dispose();
            }
        }

        private void InitSubItems()
        {
            startPosItem = new ListViewSubItem(this, downloader.GetStartPos().ToString());
            endPosItem = new ListViewSubItem(this, downloader.GetEndPos().ToString());
            processItem = new ListViewSubItem(this, downloader.GetProgress().ToString()+"%");
            rateItem = new ListViewSubItem(this, downloader.GetTransferSpeed().ToString()+ " KB/s");            

            SubItems.Clear();
            SubItems.Insert(0, startPosItem);
            SubItems.Insert(1, endPosItem);
            SubItems.Insert(2, processItem);
            SubItems.Insert(3, rateItem);
        }

        private void InitTimer()
        {
            updateTimer = new Timer();
            updateTimer.Tick += new EventHandler(UpdateTimer_Tick);
            updateTimer.Interval = 2000;
        }

        private void UpdateTimer_Tick(object sender, EventArgs e)
        {
            RefreshSegmentInfo();
        }

        private void RefreshSegmentInfo()
        {
            processItem.Text = downloader.GetProgress().ToString() + "%";
            rateItem.Text = downloader.GetTransferSpeed().ToString() + " KB/s";
        }
    }
}
